from odoo import api, fields, models


class AgriDisease(models.Model):
    _name = 'agri.disease'
    _description = '植物病害知识库'
    _inherit = ['mail.thread', 'mail.activity.mixin']

    name = fields.Char(string='病害名称', required=True, tracking=True)
    code = fields.Char(string='编号', copy=False, tracking=True)
    crop_ids = fields.Many2many('agri.crop', string='主要寄主作物', tracking=True)
    disease_type = fields.Selection(
        selection=[
            ('fungal', '真菌'),
            ('bacterial', '细菌'),
            ('viral', '病毒'),
            ('pest_damage', '虫害引起'),
            ('nutrient', '营养缺失'),
            ('physiological', '生理性'),
            ('other', '其他'),
        ],
        string='类型',
        default='fungal',
        tracking=True,
    )
    severity = fields.Selection(
        selection=[
            ('low', '低'),
            ('medium', '中'),
            ('high', '高'),
            ('critical', '爆发'),
        ],
        string='危害等级',
        default='medium',
        tracking=True,
    )
    symptoms = fields.Html(string='典型症状', sanitize=False)
    causes = fields.Text(string='病因与诱发条件')
    spread_conditions = fields.Text(string='流行条件')
    treatment = fields.Html(string='防治措施', sanitize=False)
    prevention = fields.Html(string='预防建议', sanitize=False)
    season_window = fields.Char(string='高发季节')
    monitoring_tips = fields.Text(string='巡查要点')
    recommended_products = fields.Char(string='推荐药剂/生防方案')
    reference_links = fields.Char(string='参考链接', help='使用分号分隔多个链接')
    audio_summary = fields.Text(string='语音摘要')
    image_1920 = fields.Image(max_width=1920, max_height=1920)
    active = fields.Boolean(default=True)
    tag_ids = fields.Many2many('agri.tag', string='标签', domain="[('usage', 'in', ('disease', 'common'))]")

    _sql_constraints = [
        ('agri_disease_code_unique', 'unique(code)', '病害编号必须唯一。'),
    ]

    @api.onchange('name')
    def _onchange_name(self):
        for record in self:
            if not record.audio_summary and record.name:
                record.audio_summary = f"{record.name} 的主要症状包括："

